package com.example.instantmessage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.example.instantmessage.ChatActivity;
import com.example.instantmessage.InstantMessageApplication;
import com.example.instantmessage.Msg;
import com.example.instantmessage.R;
import com.example.instantmessage.login.LoginActivity;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMMessage;

import java.util.List;

public class ChatAdapter extends ArrayAdapter<V2TIMMessage> {
    private int resourceId;
    private String friendsId;

    public ChatAdapter(@NonNull Context context, int resource, @NonNull List<V2TIMMessage> objects,String friendsId) {
        super(context, resource, objects);
        resourceId = resource;
        this.friendsId = friendsId;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        V2TIMMessage msg = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId,null);
            viewHolder = new ViewHolder();
            viewHolder.leftLayout = view.findViewById(R.id.left_layout);
            viewHolder.leftMsg = view.findViewById(R.id.left_msg);
            viewHolder.rightLayout = view.findViewById(R.id.right_layout);
            viewHolder.rightMsg = view.findViewById(R.id.right_msg);
            view.setTag(viewHolder);
        }else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        String content = "";
        if (msg.getMessage().getMessageType() == V2TIMMessage.V2TIM_ELEM_TYPE_TEXT){
            content  = msg.getTextElem().getText();
        }

        if (!msg.getMessage().getSenderUserID().equals(LoginActivity.userId)){
            viewHolder.rightLayout.setVisibility(View.GONE);
            viewHolder.leftLayout.setVisibility(View.VISIBLE);
            viewHolder.leftMsg.setText(content);
        }else {
            viewHolder.rightLayout.setVisibility(View.VISIBLE);
            viewHolder.leftLayout.setVisibility(View.GONE);
            viewHolder.rightMsg.setText(content);
        }

//        if (!msg.getMessage().getReceiverUserID().equals(friendsId)){
//            viewHolder.rightLayout.setVisibility(View.VISIBLE);
//            viewHolder.leftLayout.setVisibility(View.GONE);
//            viewHolder.rightMsg.setText(content);
//        }else {
//            viewHolder.rightLayout.setVisibility(View.GONE);
//            viewHolder.leftLayout.setVisibility(View.VISIBLE);
//            viewHolder.leftMsg.setText(content);
//        }

        return view;
    }

    class ViewHolder {
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rightMsg;

    }
}
